﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASMS.Excel2DbTool.Utils
{
    /// <summary>
    /// 仓库公司仓库面积TOP5
    /// </summary>
    public class 仓库公司仓库面积TOP5
    {
        public static void Run(string filePath, string destFileName)
        {
            DataTable dataTable = Sunboxsoft.Excel.ReadTool.ReadExcel(filePath, true);

            // 公司
            StringBuilder xValuesBuilder = new StringBuilder("[");
            StringBuilder yValuesBuilder = new StringBuilder("[");

            List<string> xValues = new List<string>();
            List<decimal> yValues = new List<decimal>();

            List<string> finalXValues = new List<string>();
            List<decimal> finalYValues = new List<decimal>();

            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string nameString = Convert.ToString(dataTable.Rows[index][1]).Trim();
                string valueString = Convert.ToString(dataTable.Rows[index][7]).Trim();

                if (string.IsNullOrEmpty(nameString)
                    || string.IsNullOrEmpty(valueString))
                    continue;

                decimal value = 0;
                if (decimal.TryParse(valueString, out value))
                {
                    bool isExist = false;
                    for (int xIndex = 0; xIndex < xValues.Count; xIndex++)
                    {
                        if (string.Equals(xValues[xIndex], nameString))
                        {
                            yValues[xIndex] += value;
                            isExist = true;
                            break;
                        }
                    }
                    if (!isExist)
                    {
                        xValues.Add(nameString);
                        yValues.Add(value);
                    }
                }
            }

            for (int index = 0; index < yValues.Count; index++)
            {
                bool isInsert = false;
                for (int fIndex = 0; fIndex < finalYValues.Count; fIndex++)
                {
                    if (finalYValues[fIndex] < yValues[index])
                    {
                        finalYValues.Insert(fIndex, yValues[index]);
                        finalXValues.Insert(fIndex, xValues[index]);
                        isInsert = true;
                        break;
                    }
                }
                if (!isInsert)
                {
                    finalYValues.Add(yValues[index]);
                    finalXValues.Add(xValues[index]);
                }
            }




            for (int index = 0; index < 10; index++)
            {
                if (index == 0)
                {
                    xValuesBuilder.AppendFormat("\"{0}\"", finalXValues[index]);
                    yValuesBuilder.AppendFormat("{0}", finalYValues[index]);
                }
                else
                {
                    xValuesBuilder.AppendFormat(",\"{0}\"", finalXValues[index]);
                    yValuesBuilder.AppendFormat(",{0}", finalYValues[index]);
                }
            }
            xValuesBuilder.Append("]");
            yValuesBuilder.Append("]");
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("{{\"xValues\":{0},\"yValues\":{1}}}", xValuesBuilder, yValuesBuilder);

            string destFilePath = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, destFileName);
            System.IO.File.WriteAllText(destFilePath, builder.ToString());
            Console.WriteLine(string.Format("转换完毕{0},count:{1}", destFilePath, dataTable.Rows.Count));
        }
    }
}